#include "stdafx.h"
#include "Material.h"


Material::Material(void) 
{ 
	material_Ka[0] = 0; material_Ka[1] = 0; material_Ka[2] = 0; material_Ka[3] = 0;
	material_Kd[0] = 0; material_Kd[1] = 0; material_Kd[2] = 0; material_Kd[3] = 0;
	material_Ks[0] = 0; material_Ks[1] = 0; material_Ks[2] = 0; material_Ks[3] = 0;
	material_Sh = 0;

	texture = NULL;
	bump = NULL;
	cube = NULL;
}

Material::Material(GLfloat Ka[4], GLfloat Kd[4], GLfloat Ks[4], GLfloat Sh)
{
	init(Ka, Kd, Ks, Sh);

	texture = NULL;
	bump = NULL;
	cube = NULL;
}

Material::~Material(void) 
{
	if( texture != NULL)
		delete texture;
	if( bump != NULL)
		delete bump;
	if( cube != NULL)
		delete cube;
}

void Material::init(GLfloat Ka[4], GLfloat Kd[4], GLfloat Ks[4], GLfloat Sh) 
{
	initKa(Ka);
	initKd(Kd);
	initKs(Ks);
	initSh(Sh);
}

void Material::initKa(GLfloat Ka[4]) 
{
	material_Ka[0] = Ka[0]; material_Ka[1] = Ka[1]; material_Ka[2] = Ka[2]; material_Ka[3] = Ka[3];
}

void Material::initKd(GLfloat Kd[4]) 
{
	material_Kd[0] = Kd[0]; material_Kd[1] = Kd[1]; material_Kd[2] = Kd[2]; material_Kd[3] = Kd[3];
}

void Material::initKs(GLfloat Ks[4]) 
{
	material_Ks[0] = Ks[0]; material_Ks[1] = Ks[1]; material_Ks[2] = Ks[2]; material_Ks[3] = Ks[3];
}

void Material::initSh(GLfloat Sh) 
{
	material_Sh = Sh;
}

void Material::use(void)
{
	glUniform1i(ShaderManager::loc_render_type, 0);

	glMaterialfv(GL_FRONT, GL_AMBIENT, material_Ka);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);
	glMaterialfv(GL_FRONT, GL_SPECULAR, material_Ks);
	glMaterialf(GL_FRONT, GL_SHININESS, material_Sh);

	if ( texture != NULL )
	{
		texture->use(GL_TEXTURE0, ShaderManager::loc_difuse_map, 0, GL_TEXTURE_2D);
		glUniform1i(ShaderManager::loc_render_type, 1);
	}
	if ( bump != NULL )
	{
		bump->use(GL_TEXTURE1, ShaderManager::loc_normal_map, 1, GL_TEXTURE_2D);
		glUniform1i(ShaderManager::loc_render_type, 2);
	}
	if ( cube != NULL )
	{
		cube->use(GL_TEXTURE0, ShaderManager::loc_cube_map, 0, GL_TEXTURE_CUBE_MAP);
		glUniform1i(ShaderManager::loc_render_type, 3);
	}
}

void Material::unuse(void)
{
	if ( bump != NULL )
		bump->unuse(GL_TEXTURE1, GL_TEXTURE_2D);
	if ( texture != NULL )
		texture->unuse(GL_TEXTURE0, GL_TEXTURE_2D);
	if ( cube != NULL )
		cube->unuse(GL_TEXTURE0, GL_TEXTURE_CUBE_MAP);
}
